package org.rency.ues.engine.crypto;

import org.rency.ues.common.UesDict;
import org.rency.ues.common.enums.EncryptType;
import org.rency.ues.engine.crypto.impl.CMSCryptor;
import org.rency.ues.engine.crypto.impl.DESCryptor;
import org.rency.ues.engine.crypto.impl.RSACryptor;

import java.util.concurrent.ConcurrentHashMap;

public class CryptoFactory {

	private final static ConcurrentHashMap<String,Cryptor> CRYPTOR_CONTAINER=new ConcurrentHashMap<String, Cryptor>();

	/**
	 * 获取加密机
	 * @param encryptType
	 * @return
     */
	public static Cryptor getCryptor(EncryptType encryptType){
		Cryptor cryptor = CRYPTOR_CONTAINER.get(encryptType.getCode());
		if(cryptor == null){
			cryptor = CryptoFactory.createCryptor(encryptType);
			if(cryptor != null){
				CRYPTOR_CONTAINER.put(encryptType.getCode(),cryptor);
			}
		}
		return cryptor;
	}

	private static Cryptor createCryptor(EncryptType encryptType){
		switch(encryptType){
			case CMS:
				return new CMSCryptor();
			case RSA:
				return new RSACryptor();
			case DES:
				return new DESCryptor();
			default:
				throw new IllegalArgumentException(String.format(UesDict.CHINESE_NOT_SUPPORT_ENCRYPT_TYPE_MSG,encryptType.getCode()));
		}
	}
	
}